package com.rk.study03;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.KeyValueLineRecordReader;
import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class KVTextDriver {

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

        args = new String[]{"D:\\hadoop_study_data\\study03\\input", "D:\\hadoop_study_data\\study03\\output"};

        // 1.加载配置文件
        Configuration configuration = new Configuration();

        // 重要！设置分隔符
        configuration.set(KeyValueLineRecordReader.KEY_VALUE_SEPERATOR, " ");

        Job job = Job.getInstance(configuration);

        // 2.加载驱动jar
        job.setJarByClass(KVTextDriver.class);

        // 3.设置mapper和reducer类
        job.setMapperClass(KVTextMapper.class);
        job.setReducerClass(KVTextReducer.class);

        // 4.设置map输出k、v类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);

        // 5.设置最终输出的k、v类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);

        // 6.设置输入和输出路径
        FileInputFormat.setInputPaths(job, new Path(args[0]));

        job.setInputFormatClass(KeyValueTextInputFormat.class);

        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        // 7.提交参数配置到YARN
        boolean result = job.waitForCompletion(true);
        System.exit(result? 0: 1);
    }
}
